package com.flyee.controller.tools.cartoon
{
	import com.flyee.controller.tools.cartoon.control.AppearanceMananger;
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.events.AbstractEvent;
	import com.flyee.events.AppearanceEvent;
	
	import flash.display.*;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class AppearanceCartoon extends EventDispatcher
	{
		
		private var listener:IEventDispatcher = null;
		private var peopleID:String = null;
		private var uuid:String = null;
		public static var count:uint = 0;
		public static var staticArray : Array = new Array();
		
		private var _isCreateComplete:Boolean = false;
		public function AppearanceCartoon(info:XML,peopleID:String,colorXML:XML = null)
		{
			this.uuid = getUUID();
			this.changeAppearance(info,peopleID,colorXML)
		}
		public function getID():String
		{
			return this.uuid;
		}
		public function getUser():String
		{
			return this.peopleID;
		}
		public static function getUUID():String
		{
			count++;
			return String(count);
		}
		public function getListenner():IEventDispatcher
		{
			return this;
		}
		public function get isCreateComplete():Boolean
		{
			return this._isCreateComplete;
		}
		/**
		 * 
		 * @param info          动画配置文件
		 * @param listener      动画截取进度监听器
		 * @param peopleID      动画所属人物ID
		 * @param colorXML      动画部件的颜色配置XML
		 * 
		 */		
		private function changeAppearance(info:XML,peopleID:String,colorXML:XML = null):void
		{
			this.peopleID = peopleID;
			AppearanceMananger.getInstance().regist(this,info);
		}
		public function dispath(e:AbstractEvent):void
		{
			this.dispatchEvent(e);
			if(e.type == AppearanceEvent.ALL_COMPLETE_APPEARANCE)
			{
				//this.clear();
				this._isCreateComplete = true;
			}
		}
		public function getAllCartoon():Array
		{
			var tempArray:Array = AppearanceMananger.getInstance().getAllAppearance(this.uuid)
			var newArray:Array = new Array();
			var templength:uint = tempArray.length;
			for(var i:int = 0; i < templength;i++)
			{
				if(tempArray[i] != null)
				{
					var newArray2:Array = new Array();
				
					var templength2:uint = tempArray[i].length;
					for(var j:int = 0; j < templength2;j++)
					{
						if(tempArray[i][j] != null)
						{
						var newArray3:Array = new Array();
						var newArray4:Array = new Array();
						var templength3:uint = tempArray[i][j].length;
						
						for(var k:int = 0; k < templength3;k++)
						{
							var tempBitmapS:Bitmap = tempArray[i][j][k];
							if(tempBitmapS)
							{
								var tempBitmap:Bitmap = new Bitmap(tempBitmapS.bitmapData.clone())
								tempBitmap.x = tempBitmapS.x
								tempBitmap.y = tempBitmapS.y;
								newArray3[k]= tempBitmap;
							}
						}
						newArray2[j] = newArray3;
						
						}
					}
					newArray[i] = newArray2;
				}
			}
//			var tempLength4:uint = newArray.length;
//			for(i=0;i< tempLength4;i++)
//			{
//				if(newArray[i] != null)
//				{
//					var tempArray4:Array = newArray[i].splice(0,1);
//					newArray[i].push(tempArray4[0])
//				}
//				
//			}
			return newArray;
		}
		
		public static function getStaticArray() : Array {
			var tmp : Array = new Array();
			for(var i : int = 0 ; i < staticArray.length ; i++){
				if(staticArray[i] != null){
					var tmp1 : Array = new Array();
					for(var j : int = 0 ; j < staticArray[i].length ; j++){
						if(staticArray[i][j] != null){
							var tmp2 : Array = new Array();
							for(var k : int = 0 ; k < staticArray[i][j].length ; k++){
								if(staticArray[i][j][k] != null){
									var imageData : BitmapData = staticArray[i][j][k] as BitmapData;
									var imageData1 : Bitmap = new Bitmap(imageData.clone());
								}
								tmp2.push(imageData1);
							}
							tmp1.push(tmp2);
						}
					}
					tmp.push(tmp1);
				}
			}
			return tmp;
		}
		
		public function removeAppearance():void
		{
			
		}
		public function clear():void
		{
			//AppearanceMananger.getInstance().removeTag(this);
			this.listener = null;
			this.peopleID = null;
		}
	}
}